package com.spt.springbootzhsx.fileOp;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.stereotype.Service;

import java.io.IOException;
@Service
public class ReadFile {
    //    读取hdfs文件系统中的文件
    public static String readFileToHdfs(String path) throws IOException {
//        获取配置
        Configuration conf = new Configuration();
//        配置
 //       conf.set("fs.defaultFS","hdfs://192.168.43.4:9000");
//        获取hdfs文件系统的操作对象
        FileSystem fs = FileSystem.get(conf);
//        具体的对文件系统的操作
        FSDataInputStream fsDataInputStream = fs.open(new Path(path));
        FileStatus status=fs.getFileStatus(new Path(path));
        byte[] buffer=new byte[Integer.parseInt(String.valueOf(status.getLen()))];
        fsDataInputStream.readFully(0,buffer);
        fsDataInputStream.close();
        fs.close();
        String s=new String(buffer);
        return s;
    }
}